<?php

namespace Cadss\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Cadss\TestBundle\Entity\Document;
use Cadss\TestBundle\Entity\Keyword;

class DefaultController extends Controller
{
  	public function indexAction(Request $request)
    {
        $repositoryDocument = $this->getDoctrine()->getRepository('CadssTestBundle:Document');          
        $files = $repositoryDocument->findAll();

        $repositoryKeyword = $this->getDoctrine()->getRepository('CadssTestBundle:Keyword');          
        $keywords = $repositoryKeyword->findAll();

        $data = array();
        $form = $this->CreateFormBuilder($data)
            ->setAction($this->generateUrl('cadss_test_search'))
            ->setMethod('GET')
            ->add('keyword', 'text', array(
                'label' => 'Keyword'))
            ->add('Add', 'submit')
            ->getForm();
        
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            
            $em->persist($keyword); 
            $em->flush();

            return $this->container
                ->get('templating')
                ->renderResponse('CadssTestBundle:Default:upload.html.twig', array(
                    'form' => $form->createView()
                ));
        }

        return $this->render('CadssTestBundle:Default:index.html.twig', array('files' => $files, 'keywords' => $keywords, 'form' => $form));
    } 
	
	public function loginAction()
    {
        return $this->render('CadssTestBundle:Default:layout.html.twig');
    } 
	
	public function keywordsAction()
    {
        $repositoryKeyword = $this->getDoctrine()->getRepository('CadssTestBundle:Keyword');          
        $keywords = $repositoryKeyword->findAll();

        return $this->render('CadssTestBundle:Default:keywords.html.twig', array('keywords' => $keywords, ));
    }
   	public function keywordsinfoAction($id){
   		$repositoryDocument = $this->getDoctrine()->getRepository('CadssTestBundle:Document');          
        $files = $repositoryDocument->findAll();
        $repositoryKeyword = $this->getDoctrine()->getRepository('CadssTestBundle:Keyword');          
        $keyword = $repositoryKeyword->findOneById($id);
   		return $this->render('CadssTestBundle:Default:keywords_info.html.twig', array('keyword' => $keyword, 'files' => $files));
   	}

    public function addkeywordAction(Request $request)
    {
    	$keyword = new Keyword();
    	$form = $this->CreateFormBuilder($keyword)
    		->add('keyword', 'text', array(
    			'label' => 'Keyword'))
    		->add('Add', 'submit')
    		->getForm();

    	$form->handleRequest($request);

    	if ($form->isValid()) {
	        $em = $this->getDoctrine()->getManager();
			
			$em->persist($keyword);	
	        $em->flush();

	        return $this->container
	        	->get('templating')
	        	->renderResponse('CadssTestBundle:Default:add_keyword.html.twig', array(
            		'form' => $form->createView()
       	 		));
	    }

    	return $this->render('CadssTestBundle:Default:add_keyword.html.twig', array('form' => $form->createView()));
    }
    public function editkeywordAction($id, Request $request){
    	$repositoryKeyword = $this->getDoctrine()->getRepository('CadssTestBundle:Keyword');          
    	$keyword = $repositoryKeyword->findOneById($id);

	   	$form = $this->CreateFormBuilder($keyword)
    		->add('keyword', 'text', array(
    			'label' => 'Keyword'))
    		->add('Edit', 'submit')
    		->getForm();

        $form->handleRequest($request);

    	if ($form->isValid()) {
	        $em = $this->getDoctrine()->getManager();
			
			//$em->persist($file);
			$keyword->setKeyword($form->get('keyword')->getData());
	        $em->flush();

	        return $this->container
	        	->get('templating')
	        	->renderResponse('CadssTestBundle:Default:edit_keyword.html.twig', array(
            		'form' => $form->createView()
       	 		));
	    }

    	return $this->render('CadssTestBundle:Default:edit_keyword.html.twig', array('keyword' => $keyword, 'form' => $form->createView()));
    }

	public function uploadAction(Request $request){

    	$document = new Document($this->getUser());
    	$document->setDUid($this->getUser()->getId());
	    $form = $this->createFormBuilder($document)
			->add('dTitel', 'text', array(
	        	'label'	=>	'Title'))
			->add('dOmschrijving', 'textarea', array(
	        	'label'	=>	'Description'))	        
	        ->add('file')
	        ->add('keywords', 'entity', array(
	        	'class' => 'CadssTestBundle:Keyword',
	        	'property' => 'keyword',
	        	'multiple' => true,
	        	'expanded' => true)) 
			->add('Upload', 'submit')
	        ->getForm();

	    $form->handleRequest($request);

	    if ($form->isValid()) {
	        $em = $this->getDoctrine()->getManager();
			
			$em->persist($document);	
	        $em->flush();
	        return $this->container->get('templating')->renderResponse('CadssTestBundle:Default:upload.html.twig', array(
            	'form' => $form->createView(), 'path' => $document->getAbsolutePath(),
        	));
	    }
		
		return $this->container->get('templating')->renderResponse('CadssTestBundle:Default:upload.html.twig', array(
            'form' => $form->createView(),'path' => $document->getAbsolutePath(),
        ));
	}
	public function editdocumentAction($id, Request $request){
		$repositoryDocument = $this->getDoctrine()->getRepository('CadssTestBundle:Document');          
        $document = $repositoryDocument->findOneById($id);

	    $form = $this->createFormBuilder($document)
			->add('dTitel', 'text', array(
	        	'label'	=>	'Title'))
			->add('dOmschrijving', 'textarea', array(
	        	'label'	=>	'Description'))
	        ->add('keywords', 'entity', array(
	        	'class' => 'CadssTestBundle:Keyword',
	        	'property' => 'keyword',
	        	'multiple' => true,
	        	'expanded' => true))         
			->add('Edit', 'submit')
	        ->getForm();

        $form->handleRequest($request);

    	if ($form->isValid()) {
	        $em = $this->getDoctrine()->getManager();
			
			//$em->persist($file);
			$document->setDTitel($form->get('dTitel')->getData());
	        $em->flush();

	        return $this->container
	        	->get('templating')
	        	->renderResponse('CadssTestBundle:Default:edit_document.html.twig', array(
            		'form' => $form->createView()
       	 		));
	    }

    	return $this->render('CadssTestBundle:Default:edit_document.html.twig', array('document' => $document, 'form' => $form->createView()));
	}
	public function deletedocumentAction($id){
        $repositoryDocument = $this->getDoctrine()->getRepository('CadssTestBundle:Document');
        $file = $repositoryDocument->findOneById($id);
        $message = "An error has occurred and the file coudn't be deleted";     
        if($file->getDUid() == $this->getUser()->getId() && $id!=0){
            $em = $this->getDoctrine()->getManager();
            $em->remove($file);
            $em->flush();
            $message = "Document successfully deleted";             
        }
        $files = $repositoryDocument->findAll();
        return $this->render('CadssTestBundle:Default:index.html.twig', array('files' => $files, 'message' => $message, ));
    }
    public function searchAction(Request $request){

    }
}
